<template>
  <div id="container">
    <div class="slider" :style="sliderStyle">
      <div class="item" v-for="n in itemsComputed" :key="n">

        <div class="discount">
          -16%
        </div>

        <div class="img-wrapper">
          <RouterLink to="/pc/goods/1">
            <q-img src="@/assets/images/BestSellers/7-a.webp">
            </q-img>
          </RouterLink>
        </div>


        <div class="detail spacing--xlarge product-item__text card__text">
          <div class="price">
            <div>
              <span>$9.00 - $20.00</span>
            </div>
          </div>

          <a href="javascript:;" class="title"><div class="title-inner"><span>Chocolate Cookie</span></div></a>

          <div class="product-item__ratings">
            <div class="star-rating" aria-label="">
              <q-rating
                  v-model="ratingModel"
                  size="20px"
                  color="black"
                  readonly
                  icon="star_border"
                  icon-selected="star"
              />
              <span class="star-rating__caption text-size--xsmall  palm-hide ">(3)</span>

            </div>
          </div>

          <div class="tag-wrapper" v-show="show">
<!--            <span class="tag">black</span>-->
<!--            <span class="tag">outdoor</span>-->
<!--            <span class="tag">people</span>-->
<!--            <span class="tag">No added sugar!</span>-->
            <p>No added sugar!</p>
          </div>
        </div>

        <div class="buy-btn">
          <q-btn no-caps outline rounded color="black" label="Buy now" size="17px" padding="12px" style="border-radius: 30px"/>

          <span class="alert alert--circle alert--blank" v-if="foot">
            View details, More goodies
          </span>
        </div>




      </div>
    </div>
    <aplusmax-select :count="items.length" :active="active" :prev="slide_left" :next="slide_right" :slide_position="slide_position"></aplusmax-select>
  </div>
</template>

<script setup lang='ts'>
import {defineProps, defineEmits, defineExpose, ref, computed, reactive} from 'vue'

defineProps({
  show: {
    default: true
  },
  foot:{
    default: true
  }
})

const items = reactive([
    [1, 2, 3, 4, 5],
    [5, 6, 7, 8, 7]
])

const itemsComputed = computed(() => {
  return items.flat()
})

const translateX = ref(0)
const active = ref(1)
const ratingModel = ref(3)

const slide_left = () => {
  if (active.value !== 1){
    translateX.value += 100; // 调整为滑动的距离
    active.value -= 1
  }
}

const slide_right = () => {
  if (active.value < items.length){
    translateX.value -= 100; // 调整为滑动的距离
    active.value += 1
  }
}

const slide_position = (position: Number) => {
  if (Array.from({ length: items.length }, (_, index) => index+1).includes(position)){
    active.value = position
    translateX.value = -(100*(active.value-1))
  }
}



const sliderStyle = computed(() => {
  return {
    transform: `translateX(${translateX.value}%)`,
  };
})
</script>

<style lang="less" scoped>
.discount{
  color: white;
  position: absolute;
  background-color: #d14545;
  top: 10px;
  right: 10px;
  z-index: 10;
  border-radius: 5px;
  padding: 2px 8px;
}

#container {
  width: 100%;
  overflow: hidden;
}

.slider {
  display: flex;
  transition: transform 0.5s ease;
}

.item {
  position: relative;
  border: 0.0625rem solid rgba(29, 29, 29, 0.15);
  border-radius: 0.625rem;
  flex: 0 0 calc(20% - 9.5px); /* 每个元素占据25%的宽度 */
  margin-right: 12px; /* 间隙 */
  //text-align: center;
  &:nth-child(5n){
    margin-right: unset;
  }
  overflow: hidden; /* 防止放大的图片溢出容器 */
  .img-wrapper{
    overflow: hidden;
    //a{
    //  justify-content: center;
    //  display: flex;
    //  .q-img{
    //    width: 94%;
    //    display: flex;
    //  }
    //}
  }
  .detail{
    padding: 25px 25px 18px 25px;
    .price{
      font-size: 20px;
      .fake{
        margin-left: 7px;
        opacity: .62;
        text-decoration: line-through;
      }
    }
    .title{
      font-size: 22px;
      font-weight: 800;
      .title-inner{
        margin-bottom: -10px;
      }
      span{
        position: relative;
        &::after {
          content: '';
          position: absolute;
          left: 0;
          bottom: 0;
          width: 0;
          height: 1px;
          background-color: #000000; /* 边框颜色 */
          transition: width 0.5s ease; /* 过渡效果 */
        }
        &:hover::after {
          width: 100%; /* 鼠标悬浮时边框宽度为100% */
        }
      }
    }
    .tag-wrapper{
      display: flex; /* 启用 flexbox 布局 */
      flex-wrap: wrap; /* 当容器内没有足够空间时，允许子元素换行 */
      gap: 3px;
      .tag{
        font-size: 14px;
        padding: 5px;
        background-color: #dedede;
        border-radius: 4px;
      }
    }
    .product-item__ratings{
      height: 6px;
      .remove-empty-space>*:last-child {
        margin-bottom: 0;
      }
      .star-rating {
        font-size: 16px;
        width: 100%;
        //height: 25px;
        display: grid;
        grid-template-columns: max-content auto;
        align-items: center;
        gap: 0.625rem;
      }
      .visually-hidden {
        clip: rect(0 0 0 0);
        clip-path: inset(50%);
        height: 1px;
        overflow: hidden;
        position: absolute;
        white-space: nowrap;
        width: 1px;
      }
      .card .star-rating__stars {
      }
      .star-rating__stars {
        background-image: url(@/assets/images/pentagram.svg);
        background-repeat: repeat-x;
        font-size: 0;
        display: inline-block;
        text-align: left;
      }
      .card .star-rating__stars-active {
      }
      .star-rating__stars-active {
        background-image: url(@/assets/images/pentagram-fill.svg);
        display: inline-block;
        font-size: 0;
        height: 16px;
        background-repeat: repeat-x;
      }
    }
  }
  .buy-btn{
    padding: 10px 25px 25px 25px;
    display: flex;
    flex-direction: column;
    justify-content: stretch;
    .q-btn{
      margin-bottom: 25px;
    }



    .alert{
      background: transparent;
      border: none;
      padding: 0;
      margin: 0;
      font-size: 13px;
      white-space: nowrap;
      &::before {
        width: 0.625rem;
        height: 0.625rem;
        margin-inline-end: 2px;
        top: 0.0625rem;
        border: 2px solid;
        opacity: .25;
      }
    }
    .alert--circle {
      line-height: 1;
    }
    .alert--circle:before {
      content: "";
      display: inline-block;
      width: 0.75rem;
      height: 0.75rem;
      border-radius: 100%;
      margin-inline-end: 5px;
      position: relative;
      top: 0.125rem;
      flex-shrink: 0;
      align-self: flex-start;
    }
  }



  .spacing--xlarge > * {
    margin-bottom: 14px;
  }
  .product-item__text {
    display: flex;
    flex-direction: column;
    justify-content: stretch;
  }
  .card__text{
    z-index: 99;
    word-wrap: break-word;
    word-break: break-word;
  }
  .text-align--left{
    text-align: start;
  }
}

.item:last-child {
  margin-right: 0; /* 最后一个元素不需要间隙 */
}
.tag-wrapper{
  margin-top: 12px;
  margin-bottom: 0 !important;
  color: #646464;
}
.price{
  margin-bottom: 12px !important;
}

.q-img:hover{
  transform: scale(1.05); /* 当鼠标悬浮时放大图片 5% */
  transition: transform 0.3s ease-in-out; /* 添加平滑的过渡效果 */
}
</style>
